package app.android.kdictionary.service;

import java.util.ArrayList;

import app.android.kdictionary.Util.HttpParser;
import app.android.kdictionary.Util.HttpRequester;
import app.android.kdictionary.Util.HttpURLMgr;
import app.android.kdictionary.aidl.ISearchService;
import app.android.kdictionary.aidl.WebViewContent;
import app.android.kdictionary.aidl.SearchResultData;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class SearchService extends Service {
	private String delim = ";"; 
	private int size=0;
	HttpURLMgr hum;
	HttpRequester hr;
	HttpParser hp;
	
	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}
	
	private final ISearchService.Stub mBinder = new ISearchService.Stub(){
		@Override
		public SearchResultData simpleSearch(String searhWord) throws RemoteException {
			hum = new HttpURLMgr();
			hr = new HttpRequester();
			hp = new HttpParser();
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();
        	
        	try {
        		hum.setSimpleSearchParams(searhWord);
        		size = Integer.parseInt(hp.getSimpleAmount(hr.request(hum.getSimpleSearchURL(), "GET", hum.getParams())));
        		hp.parseSimpleList(hr.request(hum.getSimpleSearchURL(), "GET", hum.getParams()), list);
        		
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		@Override
		public SearchResultData simpleSearchNext() throws RemoteException {
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();

        	try {
        		hum.setNextPage();
        		hp.parseSimpleList(hr.request(hum.getSimpleSearchURL(), "GET", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}
		
		
		
		@Override
		public SearchResultData sectionSearch(String searhWord, int section, String gubun) throws RemoteException {
			hum = new HttpURLMgr();
			hr = new HttpRequester();
			hp = new HttpParser();
			ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();
        	
        	try {
        		hum.setSectionSearchParams(searhWord, gubun, section);
        		size = Integer.parseInt(hp.getProverbCategoryAmount(hr.request(hum.getSectionSearchURL(section), "POST", hum.getParams())));
        		hp.parseSectionList(hr.request(hum.getSectionSearchURL(section), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}
		
		@Override
		public SearchResultData sectionSearchNext(int section) throws RemoteException {
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();

        	try {
        		hum.setNextPage();
        		hp.parseSectionList(hr.request(hum.getSectionSearchURL(section), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}
		
		
		
		@Override
		public WebViewContent todayProverb(String jaso, int number) {
        	String amount;
        	int page = 1;
        	int contentNum = 0;
        	int contentNumInPage = 0;
        	
        	hum = new HttpURLMgr();
        	hr = new HttpRequester();
        	hp = new HttpParser();
        	
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();

        	try {
        		
        		hum.setSectionSearchParams("", "1", jaso, 2);
        		amount = hp.getProverbCategoryAmount(hr.request(hum.getSectionSearchURL(2), "POST", hum.getParams()));
        		contentNum = number%Integer.parseInt(amount);
        		page = contentNum/10 + 1;
        		contentNumInPage = contentNum%10;
        		
        		hum.setPage(page);
        		hp.parseSectionList(hr.request(hum.getSectionSearchURL(2), "POST", hum.getParams()), list);
        		
        		return list.get(contentNumInPage);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	return null;
		}


		@Override
		public SearchResultData jasoSearch(String jaso) throws RemoteException {
			String[] jasoList = jaso.split(delim);
			
			hum = new HttpURLMgr();
			hr = new HttpRequester();
			hp = new HttpParser();
			ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();
        	
        	try {
        		hum.setJasoSearchParams(jasoList);
//        		size = Integer.parseInt(hp.getProverbCategoryAmount(hr.request(hum.getSectionSearchURL(section), "POST", hum.getParams())));
        		hp.parseSectionList(hr.request(hum.getUrl(), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		@Override
		public SearchResultData multiSearch(
				String[] searchText, 
				String[] table,
				String[] gubun, 
				String[] orgLang, 
				String[] techTerm, 
				String[] spCode) throws RemoteException {
			
			hum = new HttpURLMgr();
			hr = new HttpRequester();
			hp = new HttpParser();
			ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();
			
        	try {
        		hum.setMultiSearchParams(searchText, table, gubun, orgLang, techTerm, spCode);
//        		size = Integer.parseInt(hp.getProverbCategoryAmount(hr.request(hum.getSectionSearchURL(section), "POST", hum.getParams())));
        		hp.parseSectionList(hr.request(hum.getUrl(), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		@Override
		public SearchResultData indexSearch(String jamo) throws RemoteException {
			// TODO Auto-generated method stub
			hum = new HttpURLMgr();
			hr = new HttpRequester();
			hp = new HttpParser();
			ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();
        	
        	try {
        		hum.setJamoSearchParams(jamo);
//        		size = Integer.parseInt(hp.getProverbCategoryAmount(hr.request(hum.getSectionSearchURL(section), "POST", hum.getParams())));
        		hp.parseSectionList(hr.request(hum.getUrl(), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		@Override
		public SearchResultData indexSearchNext() throws RemoteException {
			// TODO Auto-generated method stub
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();

        	try {
        		hum.setNextPage();
        		hp.parseSectionList(hr.request(hum.getUrl(), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		@Override
		public SearchResultData jasoSearchNext() throws RemoteException {
			// TODO Auto-generated method stub
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();

        	try {
        		hum.setNextPage();
        		hp.parseSectionList(hr.request(hum.getUrl(), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		@Override
		public SearchResultData multiSearchNext() throws RemoteException {
			// TODO Auto-generated method stub
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();

        	try {
        		hum.setNextPage();
        		hp.parseSectionList(hr.request(hum.getUrl(), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		@Override
		public SearchResultData rangeSearch(String searchStartText,	String searchEndText) throws RemoteException {
			// TODO Auto-generated method stub
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();

        	try {
        		hum.setNextPage();
        		hp.parseSectionList(hr.request(hum.getUrl(), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		@Override
		public SearchResultData rangeSearchNext() throws RemoteException {
			// TODO Auto-generated method stub
        	ArrayList<WebViewContent> list = new ArrayList<WebViewContent>();

        	try {
        		hum.setNextPage();
        		hp.parseSectionList(hr.request(hum.getUrl(), "POST", hum.getParams()), list);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        	
        	SearchResultData resultData = new SearchResultData();
        	resultData.setWebViewSize(size);
        	resultData.setWebViewContentList(list);
        	
			return resultData;
		}

		
	};
}
